package com.kandinfo.base.common.algorithm;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;


/**
 * Created by lxb on 2015/7/15.
 */
public final class DeHtmlTag {
    // 定义script的正则表达式
    private static final String REGEX_SCRIPT = "<script[^>]*?>[\\s\\S]*?<\\/script>";
    // 定义style的正则表达式
    private static final String REGEX_STYLE = "<style[^>]*?>[\\s\\S]*?<\\/style>";
    // 定义HTML标签的正则表达式
    private static final String REGEX_HTML = "<[^>]+>";
    // 定义空格回车换行符
    private static final String REGEX_SPACE = "\\s+|\t|\r|\n";
    // 定义空格回车换行符
    private static final String REGEX_NBSP = "&nbsp;";
    // 定义中文正则
    private static final String REGEX_CHINESE = "[\u4E00-\u9FFF]";
    // 定义style的正则表达式
    private static final String REGEX_IMAGE = "<img.*[^>]*?/>";

    private static final Pattern P_SCRIPT  = Pattern.compile(REGEX_SCRIPT,  Pattern.CASE_INSENSITIVE);
    private static final Pattern P_STYLE   = Pattern.compile(REGEX_STYLE,   Pattern.CASE_INSENSITIVE);
    private static final Pattern P_HTML    = Pattern.compile(REGEX_HTML,    Pattern.CASE_INSENSITIVE);
    private static final Pattern P_NBSP    = Pattern.compile(REGEX_SPACE,   Pattern.CASE_INSENSITIVE);
    private static final Pattern P_SPACE   = Pattern.compile(REGEX_NBSP,    Pattern.CASE_INSENSITIVE);
    private static final Pattern P_CHINESE = Pattern.compile(REGEX_CHINESE, Pattern.CASE_INSENSITIVE);
    private static final Pattern P_IMAGE   = Pattern.compile(REGEX_IMAGE,   Pattern.CASE_INSENSITIVE);
    
    private DeHtmlTag() {
    }

    /**
     * @param htmlStr
     * @return 删除Html标签
     */
    public static String delHTMLTag(final String htmlStr) {
        String newHtmlStr = htmlStr;

        if (newHtmlStr != null){
        	 Matcher mScript = P_SCRIPT.matcher(newHtmlStr);
             // 过滤script标签
             newHtmlStr = mScript.replaceAll("");

        Matcher mStyle = P_STYLE.matcher(newHtmlStr);
        // 过滤style标签
        newHtmlStr = mStyle.replaceAll("");

        Matcher mHtml = P_HTML.matcher(newHtmlStr);
        // 过滤html标签
        newHtmlStr = mHtml.replaceAll("");

        // 过滤空格回车标签
        Matcher mNbsp = P_NBSP.matcher(newHtmlStr);
        newHtmlStr = mNbsp.replaceAll(" ");

             Matcher mSpace = P_SPACE.matcher(newHtmlStr);
             // 过滤空格回车标签
             newHtmlStr = mSpace.replaceAll(" ");
        }else{
        	newHtmlStr = "";
        }
       
        // 返回文本字符串
        return newHtmlStr.trim();
    }

    public static String getTextFromHtml(final String htmlStr) {
        String newHtmlStr = "";

		if (htmlStr != null) {
			String oldStr = new String(htmlStr);
			newHtmlStr = delHTMLTag(oldStr);
		}
		
		return newHtmlStr;
	}
    
    /**
     * 去除中文
     * @param htmlStr
     * @return
     */
	public static String removeChinese(final String htmlStr) {
		String newStr = htmlStr;
		if (StringUtils.isBlank(newStr))
			return newStr;

		Matcher matcher = P_CHINESE.matcher(htmlStr);
		while (matcher.find()) {
			newStr = newStr.replace(matcher.group(), "");
		}
		return newStr;
	}
	
	/**
	 * 去除img标签
	 * @param htmlStr
	 * @return
	 */
	public static String removeImg(final String htmlStr) {
		String newStr = htmlStr;
		if (StringUtils.isBlank(newStr))
			return newStr;

		Matcher matcher = P_IMAGE.matcher(htmlStr);
		while (matcher.find()) {
			newStr = newStr.replace(matcher.group(), "");
		}
		return newStr;
	}
}
